<template>
  <div class="form-single-fragment">
    <el-form
      ref="form"
      :model="formData"
      class="full-width-input"
      :rules="rules"
      :label-width="formConfig.labelWidth + 'px'"
      :size="defaultFormItemSize"
      :label-position="formConfig.labelPosition"
      @submit.native.prevent
    >
      <el-row :gutter="formConfig.gutter">
        <template v-for="widget in formConfig.formWidgetList">
          <CustomTable
            v-if="widget.widgetType === SysCustomWidgetType.Table"
            :ref="widget.variableName"
            :key="widget.id"
            :widget-config="widget"
            :form-type="formConfig.formType"
            :primary-column-name="widget.primaryColumnName"
            :flow-data="flowData"
            :is-new="true"
            :get-table-query-params="getTableQueryParams"
            @operationClick="onTableOperationClick"
          />
          <CustomUpload
            v-else-if="widget.widgetType === SysCustomWidgetType.Upload"
            :ref="widget.variableName"
            :key="widget.id"
            v-model="formData[getWidgetFieldName(widget)]"
            :widget-config="widget"
            :flow-data="flowData"
            :get-data-id="getWidgetPrimaryColumnId"
          />
          <CustomWidget
            v-else
            :ref="widget.variableName"
            :key="widget.id"
            v-model="formData[getWidgetFieldName(widget)]"
            :widget-config="widget"
            :form-config="formConfig"
            :get-dropdown-params="getDropdownParams"
          >
            <template v-for="subWidget in widget.childWidgetList">
              <CustomTable
                v-if="subWidget.widgetType === SysCustomWidgetType.Table"
                :ref="subWidget.variableName"
                :key="subWidget.id"
                :widget-config="subWidget"
                :form-type="formConfig.formType"
                :is-new="true"
                :get-table-query-params="getTableQueryParams"
                @operationClick="onTableOperationClick"
              />
              <CustomUpload
                v-else-if="subWidget.widgetType === SysCustomWidgetType.Upload"
                :ref="subWidget.variableName"
                :key="subWidget.id"
                v-model="formData[getWidgetFieldName(subWidget)]"
                :widget-config="subWidget"
                :flow-data="flowData"
                :get-data-id="getWidgetPrimaryColumnId"
              />
              <CustomWidget
                v-else
                :ref="subWidget.variableName"
                :key="subWidget.id"
                v-model="formData[getWidgetFieldName(subWidget)]"
                :widget-config="subWidget"
                :form-config="formConfig"
                :get-dropdown-params="getDropdownParams"
              />
            </template>
          </CustomWidget>
        </template>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import '@/staticDict/flowStaticDict.js'
import { OnlineFormMixins } from './onlineFormMixins.js'
import CustomUpload from '@/views/onlineForm/components/customUpload.vue'
import CustomWidget from '@/views/onlineForm/components/customWidget.vue'
import CustomTable from '@/views/onlineForm/components/customTable.vue'

export default {
  name: 'WorkflowForm',
  components: {
    CustomWidget,
    CustomTable,
    CustomUpload
  },
  mixins: [OnlineFormMixins],
  props: {
    readOnly: {
      type: [String, Boolean],
      default: false
    }
  },
  inject: ['preview'],
  data() {
    return {
      flowData: {}
    }
  },
  computed: {
    formReadOnly() {
      return this.readOnly === 'true' || this.readOnly
    }
  },
  methods: {
    getWidgetFieldName(widget) {
      if (widget && widget.relation == null) {
        return (widget.column || {}).columnName
      } else {
        return widget.relation.variableName + '__' + (widget.column || {}).columnName
      }
    },
    getDropdownParams(widget) {
      if (Array.isArray(widget.dictParamList)) {
        let params = {}
        for (let i = 0; i < widget.dictParamList.length; i++) {
          const dictParam = widget.dictParamList[i]
          if (dictParam.dictValue == null || dictParam.dictValueType == null) continue
          params = this.getParamValueObj(dictParam.dictParamName, dictParam.dictValueType, dictParam.dictValue, params)
          if (params == null) return null
        }

        return params
      } else {
        return {}
      }
    },
    getTableQueryParams(widget) {
      return {}
    },
    onTableOperationClick(operation, row, widget) {
      this.handlerOperation(operation, row, widget)
    },
    setFormData(formData, oneToManyData, flowData) {
      this.flowData = flowData
      this.formData = {
        ...this.formData,
        ...formData
      }
      this.setOneToManyRelationData(oneToManyData)
    },
    setOneToManyRelationData(oneToManyRelationList) {
      if (oneToManyRelationList != null) {
        Object.keys(oneToManyRelationList).forEach(relationVariableName => {
          if (Array.isArray(this.tableWidgetList)) {
            for (let i = 0; i < this.tableWidgetList.length; i++) {
              const tableWidget = this.tableWidgetList[i]
              if (tableWidget.relation.variableName === relationVariableName) {
                const temp = this.$refs[tableWidget.variableName]
                const tableImpl = Array.isArray(temp) ? temp[0] : temp
                if (tableImpl) {
                  tableImpl.getTableWidget().dataList = oneToManyRelationList[relationVariableName]
                }
                break
              }
            }
          }
        })
      }
    },
    getVariableData(variableList) {
      let variableData
      if (Array.isArray(variableList) && variableList.length > 0) {
        variableList.forEach(variable => {
          if (!variable.builtin) {
            const column = this.columnMap.get(variable.bindColumnId)
            const relation = this.relationMap.get(variable.bindRelationId)
            if (column != null) {
              if (variableData == null) variableData = {}
              let key = relation == null ? '' : relation.variableName + '__'
              key += column.columnName
              variableData[variable.variableName] = this.formData[key]
            }
          }
        })
      }
      return variableData
    },
    getFormData() {
      return new Promise((resolve, reject) => {
        this.$refs.form.validate(valid => {
          if (!valid) return reject()
          const tempObj = {}
          const that = this
          const oneToOneRelationObj = {}
          let hasMasterData = false
          function getFlowWidgetData(widget) {
            if (widget == null || widget.readOnly || widget.disabled) return
            if (widget.relation != null) {
              if (tempObj.slaveData == null) tempObj.slaveData = {}
              if (widget.widgetType === that.SysCustomWidgetType.Table) {
                const tableData = that.getRelationTableData(widget)
                if (tableData != null) {
                  tempObj.slaveData[widget.relation.relationId] = tableData
                }
              } else {
                oneToOneRelationObj[widget.relation.relationId] = widget.relation
              }
            } else {
              hasMasterData = true
            }

            if (Array.isArray(widget.childWidgetList)) {
              widget.childWidgetList.forEach(subWidget => {
                getFlowWidgetData(subWidget)
              })
            }
          }
          // 一对多关联数据
          if (Array.isArray(this.formConfig.formWidgetList)) {
            this.formConfig.formWidgetList.forEach(widget => {
              getFlowWidgetData(widget)
            })
          }
          // 主表数据
          if (hasMasterData && this.masterTable) {
            this.masterTable.columnList.forEach(column => {
              if (tempObj.masterData == null) tempObj.masterData = {}
              tempObj.masterData[column.columnName] = this.formData[column.columnName]
            })
          }
          // 一对一关联数据
          Object.keys(oneToOneRelationObj).forEach(relationId => {
            const relation = oneToOneRelationObj[relationId]
            if (relation && relation.slaveTable && Array.isArray(relation.slaveTable.columnList)) {
              relation.slaveTable.columnList.forEach(column => {
                const value = that.formData[relation.variableName + '__' + (column || {}).columnName]
                if (tempObj.slaveData[relationId] == null) tempObj.slaveData[relationId] = {}
                tempObj.slaveData[relationId][column.columnName] = value
              })
            }
          })

          resolve(tempObj)
        })
      })
    }
  }
}

</script>

<style scoped>
</style>
